package cn.kwq.project_manger.service.impl;

import cn.kwq.project_manger.pojo.Notice;
import cn.kwq.project_manger.pojo.QNotice;
import cn.kwq.project_manger.repostitory.NoticeRepository;
import cn.kwq.project_manger.service.NoticeService;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: kwq
 * @Date: 2023/02/02/16:10
 * @Description:
 */
@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    NoticeRepository noticeRepository;
    @Autowired
    JPAQueryFactory jpaQueryFactory;

    @Override
    public List<Notice> getAllNotice() {
        QNotice notice=QNotice.notice;
        return jpaQueryFactory
                .selectFrom(notice)
                .where(notice.isDelete.eq(0))
                .fetch();
    }

    @Override
    public Notice saveNotice(String dto) {
        return noticeRepository.save(Notice
                .builder()
                        .content(dto)
                        .isDelete(0)
                .build());

    }

    @Override
    @Transactional
    public List<Notice> deleteNotice(Long noticeId) {
        QNotice notice=QNotice.notice;
        jpaQueryFactory.update(notice)
                .set(notice.isDelete,1)
                .execute();
        return jpaQueryFactory
                .selectFrom(notice)
                .where(notice.isDelete.eq(0))
                .fetch();
    }
}
